package org.reliablesource.prism.gef.editor.figures;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.geometry.Rectangle;
import org.reliablesource.prism.gef.editor.PrismEditorPlugin;

/**
 *
 *  PointerFigure.java
 *  Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 *
 *	Created on: Apr 25, 2006 
 *  @author Dennis Park <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 */

public class PointerFigure extends ImageFigure{

	boolean selected = false;
	boolean hasFocus = false;
	Label tip;
	public PointerFigure(){
		super();
		setBorder(new MarginBorder(3));
		try{
		setImage(PrismEditorPlugin.getImageDescriptor("icons/pointer.gif").createImage());
		}catch(final NullPointerException _npe){
			setImage(PrismEditorPlugin.getImageDescriptor("icons/pointer.GIF").createImage());
		}
		tip = new Label();
		setToolTip(tip);
	}
	
	private Rectangle getSelectionRectangle() {
		
		Rectangle bounds = new Rectangle(getBounds().getCopy());
		
		translateToParent(bounds);
		bounds.intersect(getBounds());
		return bounds;
	}
	
	/**
	 * @see org.eclipse.draw2d.Label#paintFigure(org.eclipse.draw2d.Graphics)
	 */
	protected void paintFigure(Graphics graphics) {
		if (selected) {
			graphics.pushState();
			graphics.setBackgroundColor(ColorConstants.lightBlue);
			graphics.fillRectangle(getSelectionRectangle());
			graphics.popState();
			graphics.setForegroundColor(ColorConstants.gray);
		}
		if (hasFocus) {
			graphics.pushState();
			graphics.setXORMode(true);
			graphics.setForegroundColor(ColorConstants.menuBackgroundSelected);
			graphics.setBackgroundColor(ColorConstants.gray);
			graphics.drawFocus(getSelectionRectangle().resize(-1, -1));
			graphics.popState();
		}
		tip.setText(this.getLocation().x + ", " + this.getLocation().y);
		super.paintFigure(graphics);
	}

	/**
	 * Sets the selection state of this SimpleActivityLabel
	 * @param b true will cause the label to appear selected
	 */
	public void setSelected(boolean b) {
		selected = b;
		repaint();
	}

	/**
	 * Sets the focus state of this SimpleActivityLabel
	 * @param b true will cause a focus rectangle to be drawn around the stickyNote of the Label
	 */
	public void setFocus(boolean b) {
		hasFocus = b;
		repaint();
	}
}
